#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
# get data from SFX
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM);

use avsCommon;

#use re 'debug';

my ($content, $response, $request, $url);

dbsetup();

my $begperiod = myperiod($begyear, $begmonth);
my $endperiod = myperiod($endyear, $endmonth);

# go through all logins 
do {
  getStats ();
} while ($sthlogin->fetch);

dbshutdown();

# uses vendorId, collegeid, user, pass. retrieves stats and updates db
sub getStats {
	my $ua = new LWP::UserAgent(keep_alive=>1);

	# login for college
	$url = 'http://sfx3.linccweb.org:9003/sfxadmin/' . uc($username) . '/sfxctrl.cgi';
	$response = $ua->request(POST $url,
		[	'function'				=> 'login',
			'login.username' 	=> $username,
			'login.password' 	=> $password,
			'redirect'				=> '' ] ) ;

	if (!$response->is_success) {
		die "#error:$0: POST login $url" . $response->status_line;
	};

	$content=$response->decoded_content;
	
	# get the sid from the response
	$content =~ /name\=sid value\=\"(?<sid>.*?)\"/s;
	my $sid = $+{sid};

	# ask for the report 
	$url = 'http://sfx3.linccweb.org:9003/' . uc($username) . "/sfxadmin/stats_main.cgi?query=4&perform=execute&sid=$sid&scope=month&dateType=absolute&relativeDate=today&fromDate=" .
		sprintf('%04d-%02d-%02d', @begdate) . '&__from_month=' . sprintf('%02d', $begmonth) . '&__from_day=' . sprintf('%02d', $endday) . "&__from_year=$begyear&toDate=" .
		sprintf('%04d-%02d-%02d', @enddate) . '&__from_month=' . sprintf('%02d', $endmonth) . '&__from_day=' . sprintf('%02d', $endday) . "&__from_year=$endyear&" .
		'&if=DEFAULT&pff=html&uf=&ff=&ipf=&format=txt&delivery=browser&email=';
	$response = $ua->request(GET $url);

	# here is the statistical data
	$content=$response->decoded_content;

	# save contents for possible debugging
	savecontent('SFX', $username, $content);

	# save the returned months. should be equal to the requested months, but safer to see what was actually returned.
	my @periods;
	while ($content =~ /\tRequests (?<period>\d{4}-\d{2})/g) {
		push(@periods, $+{period});
	}

	# split out the actual data lines
	my @contents = split ("\n", $content);
	
	# precompile regexs used to tear apart data
	my $reDatasource = qr/^(?<datasource>.*?)\t/;
	my $reValues = qr/\t(?<requests>\d*)\t(?<clickthru>\d*)/;

	foreach $content (@contents[2..$#contents]) {

		if ($content =~ $reDatasource) {
			my $datasource = $+{datasource};
			
			$content .= "\n";

			foreach (@periods) {

				my $period = $_.'-01';

				#get monthly data from table
				if ($content =~ /$reValues/gc) {
					my $requests = $+{requests};
					my $clickthru = $+{clickthru};
					print "$vendorid $collegeid $period $datasource $requests $clickthru\n";
					$sthstats->execute($vendorid, $collegeid, $datasource, $period, $requests, $REQUESTS)
						or die "#error: sthstats 1 execute failed, check end of log";
					$sthstats->execute($vendorid, $collegeid, $datasource, $period, $clickthru, $CLICKTHRU)
						or die "#error: sthstats 2 execute failed, check end of log";
				}
			}
		}
	}

	# logout the session
	$url = "http://sfx3.linccweb.org:9003/sfxadmin/FLCC0200/sfxctrl.cgi?sid=$sid&function=logout";
	$response = $ua->request(GET $url);
};
